<?php

/**
 * 商品订单记录模型
 * File Name：MoneyPositionOrderModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-14 9:26:56
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MoneyPositionOrderModel extends BaseModel {

    public function tableName() {
        return '{{money_position_order}}';
    }

    public function rules() {
        return array(
            array('order_sn', 'required', 'message' => '订单编号不能为空！'),
            array('order_sn', 'unique', 'message' => '该订单编号已经存在！'),
            array('uid', 'required', 'message' => '会员UID不能为空！'),
            array('order_title', 'required', 'message' => '订单标题不能为空！'),
            array('price', 'required', 'message' => '商品价格不能为空！'),
            array('money', 'required', 'message' => '支付金额不能为空！'),
            array('payment_types', 'required', 'message' => '订单支付类型不能为空！'),
            //无需验证
            array('type,add_time,alipay_sn,recommend_uid,product_id,goods_name,status,goods_bm_id', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function createOrders($data) {
        if (empty($data) || !is_array($data)) {
            $this->addError('', '订单数据不能为空！');
            return false;
        }
        $this->attributes = $data;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return $this->primaryKey;
        } else {
            return false;
        }
    }

    public function getOrders($id) {
        $data = $this->findByPk($id);
        if (empty($data)) {
            return false;
        }
        return $data->attributes;
    }

    public function getByOrderSN($order_sn, $rObject = false) {
        $data = $this->find('order_sn = :order_sn', array('order_sn' => $order_sn));
        if (empty($data)) {
            return false;
        }
        return $rObject ? $data : $data->attributes;
    }

    public function getMenberAds($uid, $page_num = 15) {
        $uid = (int) $uid;
        if (!$uid) {
            return false;
        }

        $criteria = new CDbCriteria();
        $criteria->addCondition('uid = :uid');
        $criteria->params = array(
            ':uid' => $uid,
        );
        $page_num = (int) $page_num < 0 ? 15 : $page_num;

        $count = $this->model()->count($criteria);
        $pager = new CPagination($count);
        $pager->pageSize = $page_num;
        $pager->applyLimit($criteria);
        $ads = $this->findAll($criteria);
        return array($ads, $pager);
    }

    public static function convertAdStatus($stutus_id) {
        switch ($stutus_id) {
            case 0:
                return '<span class="red">未付款，不可用 </span>';
                break;
            case 1:
                return '已付款，可使用';
                break;
            case 2:
                return '已付款，已使用';
                break;
            default:
                return "未知";
                break;
        }
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->add_time = time();
            $this->alipay_sn = '';
            $this->status = 0;
        } else {
            
        }
        return true;
    }

}
